package com.iwunu.gateway.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.iwunu.gateway.constant.ResultCodeEnum;
import com.iwunu.gateway.entity.FaceLock;
import com.iwunu.gateway.service.BluetoothDataService;
import com.iwunu.gateway.service.JpaService;
import com.iwunu.gateway.service.ProtocolDataService;
import com.iwunu.gateway.util.ResultUtil;
import com.iwunu.iot.common.util.Result;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
/**
 * @author kinfeng
 * @date 2021/7/30 5:48 PM
 * @description
 */
@Service
public class BluetoothDataServiceImpl implements BluetoothDataService {
    @Autowired
    private JpaService jpaService;
    @Autowired
    private ProtocolDataService protocolDataService;
//    @Autowired
//    private PitRecordService pitRecordService;
    
    

    public Result setManagerPwd(String mac,Integer baseCode, Integer pwd,String type) {
        Integer adminPwd=12345678;
        return new Result(ResultCodeEnum.OK.getCode(),ResultCodeEnum.OK.getDesc(),protocolDataService.setlockAdminPwd(mac,adminPwd.toString().trim(),pwd.toString().trim()));
        
    }
    public Result setManagerPwd(String mac,Integer baseCode, Integer pwd,Integer oldpwd) {
    	if(oldpwd==null) {
    		oldpwd = 12345678;
    	}
        return new Result(ResultCodeEnum.OK.getCode(),ResultCodeEnum.OK.getDesc(),protocolDataService.setlockAdminPwd(mac,oldpwd.toString().trim(),pwd.toString().trim()));
        
    }
    /**
     * *改管理密码
     * @param id
     * @param oldpwd
     * @param newpwd
     * @return
     */
    public Result updateManagerPwd(Long id, Integer oldpwd,Integer newpwd) {
    	FaceLock flock = jpaService.findById(FaceLock.class,id);
        String adminPwd=flock.getAdminPassword();
      if(flock.getAdminPassword().equals(oldpwd.toString())){
       	 return ResultUtil.generateResult(ResultCodeEnum.ADMINPWD_ERROR.getCode(),ResultCodeEnum.ADMINPWD_ERROR.getDesc());
       }
        return new Result(ResultCodeEnum.OK.getCode(),ResultCodeEnum.OK.getDesc(),protocolDataService.setlockAdminPwd(flock.getMac(),adminPwd.toString().trim(),newpwd.toString().trim()));
    }
   /**
    **添加密码
    * @param id
    * @param basecode
    * @param pwd
    * @param startTime
    * @param endTime
    * @return
    */
	public Result addBluetoothPwd(Integer basecode, Long id, Integer pwd, String type) {
//		 Integer pitNum = null;
//        if(type.toUpperCase().contains("O")||type.toUpperCase().contains("C")||type.toUpperCase().contains("D")||type.toUpperCase().contains("F")) {
//        	Result result = pitRecordService.getPitNum(id,1);
//        	pitNum  = (Integer) result.getData();
//        }
//        return new Result(ResultCodeEnum.OK.getCode(),ResultCodeEnum.OK.getDesc(),protocolDataService.addBluetoothPwd(basecode,pitNum,pwd),pitNum);
		return null;
	}
	   /**
	    * 	添加密码
	    * @param id
	    * @param basecode
	    * @param pwd
	    * @param startTime
	    * @param endTime
	    * @return
	    */
		public Result addBluetoothPwd(Integer basecode, Integer pitNum, Integer pwd) {
	        return new Result(ResultCodeEnum.OK.getCode(),ResultCodeEnum.OK.getDesc(),protocolDataService.addBluetoothPwd(basecode,pitNum,pwd));
		}
	   /**
	    * 4.4.1添加密码
	    * @param id
	    * @param basecode
	    * @param pwd
	    * @param startTime
	    * @param endTime
	    * @return
	    */
		public Result addTempPwd(Integer basecode, Long id, Integer pwd, String type,String startTime,String endTime) {
//			 Integer pitNum = null;
//	        if(type.toUpperCase().contains("O")||type.toUpperCase().contains("C")||type.toUpperCase().contains("D")||type.toUpperCase().contains("F")) {
//	        	Result result = pitRecordService.getPitNum(id,1);
//	        	pitNum  = (Integer) result.getData();
//	        }
//	        return new Result(ResultCodeEnum.OK.getCode(),ResultCodeEnum.OK.getDesc(),protocolDataService.addTempPwd(basecode,pitNum,pwd,startTime,endTime),pitNum);
			return null;
		}
		   /**
		    * 4.4.1.1添加密码
		    * @param id
		    * @param basecode
		    * @param pwd
		    * @param startTime
		    * @param endTime
		    * @return
		    */
			public Result addTempPwd(Integer basecode, Integer pitNum, Integer pwd,String startTime,String endTime) {
//		        return new Result(ResultCodeEnum.OK.getCode(),ResultCodeEnum.OK.getDesc(),protocolDataService.addTempPwd(basecode,pitNum,pwd,startTime,endTime),pitNum);
				return null;
			}
	/**
	  * 修改密码
	  * @param id
	  * @param basecode
	  * @param pwd
	  * @param startTime
	  * @param endTime
	  * @return
	*/
	public Result updateBluetoothPwd(Integer basecode, Long id, Integer pwd, Integer oldPwd,String type,Integer pitNum) {
//	     return new Result(ResultCodeEnum.OK.getCode(),ResultCodeEnum.OK.getDesc(),protocolDataService.updateBluetoothPwd(basecode,pitNum,pwd),pitNum );
		return null;
	}
	
	/**
	 * 	添加卡模式
	 * @param pitNum

	*/
	public Result addCard( Long id,String startTime,String endTime) {
//			Integer pitNum = null;
//	        Result result = pitRecordService.getPitNum(id,2);
//	        pitNum  = (Integer) result.getData();
//	        return new Result(ResultCodeEnum.OK.getCode(),ResultCodeEnum.OK.getDesc(),protocolDataService.addCard(pitNum,startTime,endTime),pitNum);
		return null;
		}
	
	/**
	 * 	添加卡模式
	 * @param pitNum

	*/
	public Result addCard(Integer pitNum ,String startTime,String endTime) {
	        return new Result(ResultCodeEnum.OK.getCode(),ResultCodeEnum.OK.getDesc(),protocolDataService.addCard(pitNum,startTime,endTime),pitNum.toString());
		}	

    /**
     * 	删除指纹
     */
    public Result delFinger(Integer pitNum) {
    	return new Result(ResultCodeEnum.OK.getCode(),ResultCodeEnum.OK.getDesc(),protocolDataService.delFinger(pitNum));	
    }
    /**
     *	 添加指纹
     * @param pitNum
     * @return
     */
    public  Result addNewFingerprint(Long id,String startTime,String endTime){
//		Integer pitNum = null;
//        Result result = pitRecordService.getPitNum(id,3);
//        pitNum  = (Integer) result.getData();
//    	return new Result(ResultCodeEnum.OK.getCode(),ResultCodeEnum.OK.getDesc(),protocolDataService.addNewFingerprint(pitNum,startTime,endTime),pitNum);	
    	return null;
    } 
    
    /**
     *	 添加指纹
     * @param pitNum
     * @return
     */
    public  Result addNewFingerprint(Integer pitNum,String startTime,String endTime){
    	return new Result(ResultCodeEnum.OK.getCode(),ResultCodeEnum.OK.getDesc(),protocolDataService.addNewFingerprint(pitNum,startTime,endTime),pitNum.toString());	
    } 
    
    /**
     * 	删除密码
     */
    public Result delPwd(Integer baseCode, String pwd,Integer pitNum) {
    	if(pitNum!=null) {
    		return new Result(ResultCodeEnum.OK.getCode(),ResultCodeEnum.OK.getDesc(),protocolDataService.delPitNumPwd(pitNum));
    	}
        return  new Result(ResultCodeEnum.OK.getCode(),ResultCodeEnum.OK.getDesc(),protocolDataService.delPwd(baseCode, pwd));
    }
    /**
     * 	删除卡
     */
    public Result delCardNo(Integer  baseCode, String cardNo ,Integer pitNum) {
    	if(pitNum!=null) {
    		return new Result(ResultCodeEnum.OK.getCode(),ResultCodeEnum.OK.getDesc(),protocolDataService.delPitNumCardNo(pitNum));
    	}
        return  new Result(ResultCodeEnum.OK.getCode(),ResultCodeEnum.OK.getDesc(),protocolDataService.delCardNo(baseCode, cardNo));
    }

   
    /**
     *  开锁第一步
     * @param baseCode
     * @param random
     * @return
     */
    public Result openLockStep1(String baseCode) {
    	return new Result(ResultCodeEnum.OK.getCode(),ResultCodeEnum.OK.getDesc(), protocolDataService.openLockStep1(baseCode));
    }
    
    /**
     *  开锁第二步
     * @param baseCode
     * @param randomN
     * @return
     */
    public Result openLockStep2(String baseCode,String randomN,String subType) {
        	return new Result(ResultCodeEnum.OK.getCode(),ResultCodeEnum.OK.getDesc(), protocolDataService.openLockStep2(baseCode,randomN,subType));
     }
    
    


    public static void main(String[] args) {
        String resultData = "AAAAB102010A12" ;
        Integer status = Integer.valueOf(resultData.substring(8,10),16);
        System.out.println(status);

        Integer N = Integer.valueOf(resultData.substring(10,12),16);
    }




}
